package njiandan.mvc.core;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(asyncSupported = true)
public class NjiandanServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private Dispatcher dispatcher;
	private StaticFileHandler staticFileHandler;
	private ActionExecutor actionExecutor;

	@Override
	public void init(final ServletConfig servletConfig) throws ServletException {
		super.init(servletConfig);
		this.dispatcher = new Dispatcher();
		this.actionExecutor = new ActionExecutor();

		Config config = new Config() {
			public String getInitParameter(String name) {
				return servletConfig.getInitParameter(name);
			}

			public ServletContext getServletContext() {
				return servletConfig.getServletContext();
			}
		};

		this.dispatcher.init(config);
		this.actionExecutor.init(config);

		this.staticFileHandler = new StaticFileHandler(servletConfig);
	}

	@Override
	public void service(ServletRequest req, ServletResponse resp)
			throws ServletException, IOException {
		HttpServletRequest httpReq = (HttpServletRequest) req;
		HttpServletResponse httpResp = (HttpServletResponse) resp;
		String method = httpReq.getMethod();

		if ("GET".equals(method) || "POST".equals(method)
				|| "PUT".equals(method) || "DELETE".equals(method)) {
			/* 进入处理请求 */
			Action action = dispatcher.dispatch(httpReq);
			if (action != null) {
				actionExecutor.executeAction(httpReq, httpResp, action);
			} else {
				staticFileHandler.handle(httpReq, httpResp);
			}
			return;
		}

		httpResp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
	}

	@Override
	public void destroy() {
		this.dispatcher.destroy();
	}
}